<?php

namespace Db;

/**
 * mysql操作类
 *
 * @author zxcvdavid@gmail.com
 *
 */
class Mysql implements DbInterface {

    private static $_instances;
    private $_dbh;//PDO对象
    private $_sth;//SQL执行对象
    private $_sql;
    private $_conf;
    private $field="*";
    private $where = array();
    private $order;
    private $tabName;     

    public function __construct($conf, $tableName = "") {
        $this->setConf($conf);
        $this->tabName = $tableName;
        try {
            $idx = md5($this->_conf['db_host'] . $this->_conf['db_name']);
            if (!isset(self::$_instances[$idx])) {
                $db = new \PDO('mysql:dbname=' . $this->_conf['db_name'] . ';host=' . $this->_conf['db_host'], $this->_conf['db_user'], $this->_conf['db_pwd'], array(\PDO::ATTR_PERSISTENT => true));
                $db->query('SET NAMES ' . $this->_conf['db_charset']);
                self::$_instances[$idx] = $db;
                $this->_dbh = $db;
            }
            return self::$_instances[$idx];
        } catch (PDOException $e) {
            echo '<pre>';
            echo '<b>Connection failed:</b> ' . $e->getMessage();
            die();
        }
    }

    function setConf($dbhost = array()) {
        $this->_conf = $dbhost;
    }

    function field($field = "*") {
        if (is_array($field)) {
            $this->field = "";
            foreach ($field as $v) {
                $this->field .= $v . ",";
            }
            $this->field = rtrim($this->field, ",");
        } elseif (is_string($field)) {
            $this->field = $field;
        }
        return $this;
    }

    function where($where, $values = array()) {
        if (is_array($where)) {
            foreach ($where as $key => $v) {
                $this->where[] = " " . $key . "='" . $v . "' ";
            }
        } else {
            $this->where[] = $where;
        }                           
        return $this;
    }

    function order($order) {
        ;
    }

    function setTable($tabName) {
        ;
    }

    function halt($msg = '', $sql = '') {
        $error_info = $this->_sth->errorInfo();
        $s = '<pre>';
        $s .= '<b>Error:</b>' . $error_info[2] . '<br />';
        $s .= '<b>Errno:</b>' . $error_info[1] . '<br />';
        $s .= '<b>Sql:</b>' . $this->_sql;
        exit($s);
    }

    function execute($sql, $values = array()) {
        $sth = $this->_dbh->prepare($sql);
        $sth->execute($values); 
        $this->sth=$sth;
        return $sth;
    }

    function getAll($sql, $values = array(), $fetch_style = \PDO::FETCH_ASSOC) {
        $sql="select ".$this->field." from ".$this->tabName."";
        if(!empty($where)){
            $where="";
            foreach ($where as $key=>$v){
                $where.= $v." and ";
            }
            $where.= "1=1";
            $sql.=$where;
        }
        
echo $sql;exit;        
        return $this->execute($sql, $values)->fetchAll($fetch_style);
    }

    function getCol($sql, $params = array(), $column_number = 0) {
        
    }

    function getRow($sql, $values = array(), $fetch_style = \PDO::FETCH_ASSOC) {
        $sth = $this->execute($sql, $values);
        $count = $sth->rowCount($fetch_style);
        return $count;
    }

    function getRowCount() {
        $sth = $this->execute($sql, $values);
        $count = $sth->rowCount($fetch_style);
        return $count;
    }

    function getOne($sql, $values = array(), $fetch_style = \PDO::FETCH_ASSOC) {
        $sth = $this->execute($sql, $values);
        $rs = $sth->fetch($fetch_style);
        return $rs;
    }

    function insert($table = null, $data = null, $returnStr = false) {
        
    }

    function update($table, $data, $where) {
        
    }

    function delete($table, $where) {
        
    }

    /*
     * 处理事务
     */

    function transaction($sql) {
        try {
            $this->_dbh->beginTransaction();
            $this->_dbh->exec($sql);
            $this->_dbh->commit();
        } catch (PDOException $ex) {
            $this->_dbh->rollBack();
        }
    }

    function close() {
        unset($this->_instances);
        unset($this->_dbh);
    }

}
